capture	log close
log using MAR09-Desc* graph,  replace text

//  program:    Stata 
//  task:       Cross-Sectional Models 
//  project:    Markets  

version
clear all
macro drop _all
set linesize 80
set more off
local tag " 06-29-24| Cleaned 06-29-24"
local file "MAR09-Desc* graph"
local note "|`tag' | `file'"
local opt "noparen sideway excel noaster  bdec(2)  sdec(2)  pdec(3)   adec(2) e(r2) stats(coef se pval)"
local dv "lrhom"
local iv "fraser"
local iv2 "infantmort" 
local cont "edu unemp  popdense perurban  sexratio"

//	#0
//	loading data 
use MAR08-CondtionModel.dta, clear 

//	#1
//	Sample
tab nation 

//	#2
//	correlations 
cor `dv' `iv' `iv2' `cont' 

//	#3
//	descriptive
tabstat `dv' `iv' `iv2' `cont' rhom, stats(min max p50 mean sd) 

//	#4
//	WVS measure
preserve 
use WVS-06-29-2024.dta, clear 
tab S002VS
tab S002VS, nolab
drop if S002VS<4
tab S002VS
tab A165
tab A165, nolab
drop if A165<0
gen trust=1 if A165==1
replace trust=0 if A165==2
gen notrust=1 if A165==2
replace notrust=0 if A165==1
tab COW_ALPHA
collapse (mean) trust notrust (firstnm) COW_ALPHA COW_NUM, by( S003)
sum trust
sum trust notrust
rename COW_ALPHA countrycode 
gen CID=.
do 00-CID-CountryCode.do
tab S003 if CID==.
replace CID=S003 if CID==.
save Trust.dta, replace
restore
merge m:m CID using Trust.dta
drop _merge 

//	#5
//	corr 
pwcorr `dv' `iv' `iv2' `cont' KOFGI private_sp public_sp trust notrust 

//	#6
//	* graph 1:  Fraser homicide 
replace nation="United Kingdom" if CID==826
replace nation="Venezuela" if CID==862

* graph twoway scatter lrhom fraser
* graph export `file'-lrhomfraser.png, replace 

//	#7
//	Label CID 
do label.do
label values CID CID
tab CID

//	#8
//	* graphing by region 
foreach cid in 32	44	52	76	84	152	170	188	214	218	222	320	328	388	484	558	591	600	604	780	858	862 {
 local v : label (CID) `cid'
 * graph twoway connected lrhom fraser year if CID==`cid', title("`v'") name(n_`cid', replace)
 * graph twoway scatter lrhom fraser if CID==`cid', title("`v'") name(t_`cid', replace)
 
 }
* graph combine n_32 n_44 n_52 n_76 n_84 n_152 n_170 n_188 n_214 n_218 n_222 n_320 n_328 n_388 n_484 n_558 n_591 n_600 n_604 n_780 n_858 n_862, altshrink 
* graph export `file'-NLAC.png, replace 

* graph combine t_32 t_44 t_52 t_76 t_84 t_152 t_170 t_188 t_214 t_218 t_222 t_320 t_328 t_388 t_484 t_558 t_591 t_600 t_604 t_780 t_858 t_862, altshrink 
* graph export `file'-TLAC.png, replace 

foreach cid in 48	51	96	144	196	268	376	392	398	400	410	414	417	458	512	608	634	702	760	764	792 {
 local v : label (CID) `cid'
 * graph twoway connected lrhom fraser year if CID==`cid', title("`v'") name(n_`cid', replace)
 * graph twoway scatter lrhom fraser if CID==`cid', title("`v'") name(t_`cid', replace)
 
 }
* graph combine n_48 n_51 n_96 n_144 n_196 n_268 n_376 n_392 n_398 n_400 n_410 n_414 n_417 n_458 n_512 n_608 n_634 n_702 n_760 n_764 n_792
* graph export `file'-NAsia.png, replace 
* graph combine t_48 t_51 t_96 t_144 t_196 t_268 t_376 t_392 t_398 t_400 t_410 t_414 t_417 t_458 t_512 t_608 t_634 t_702 t_760 t_764 t_792
* graph export `file'-TAsia.png, replace 

foreach cid in 8	40	56	100	191	203	208	233	246	250	276	300	348	352	372	380	428	440	442	470	498	499	528	578	616	620	642	643	688	703	705	724	752	756	804	807	826 {
	local v : label (CID) `cid'
	* graph twoway connected lrhom fraser year if CID==`cid', title("`v'") name(n_`cid', replace)
 * graph twoway scatter lrhom fraser if CID==`cid', title("`v'") name(t_`cid', replace)
}
 * graph combine n_8 n_40 n_56 n_100 n_191 n_203 n_208 n_233 n_246 n_250 n_276 n_300 n_348 n_352 n_372 n_380 n_428 n_440 n_442 n_470 n_498 n_499 n_528 n_578 n_616 n_620 n_642 n_643 n_688 n_703 n_705 n_724 n_752 n_756 n_804 n_807 n_826 
 * graph export `file'-NEurope.png, replace 

* graph combine t_8 t_40 t_56 t_100 t_191 t_203 t_208 t_233 t_246 t_250 t_276 t_300 t_348 t_352 t_372 t_380 t_428 t_440 t_442 t_470 t_498 t_499 t_528 t_578 t_616 t_620 t_642 t_643 t_688 t_703 t_705 t_724 t_752 t_756 t_804 t_807 t_826 
 * graph export `file'-TEurope.png, replace 


foreach cid in 124 840 480 710 818 36 242 554 {
	local v : label (CID) `cid'
	* graph twoway connected lrhom fraser year if CID==`cid', title("`v'") name(n_`cid', replace)
 * graph twoway scatter lrhom fraser if CID==`cid', title("`v'") name(t_`cid', replace)
}
* graph combine n_124  n_840  n_480  n_710  n_818  n_36  n_242  n_554
 * graph export `file'-NOther.png, replace 

* graph combine t_124  t_840  t_480  t_710  t_818  t_36  t_242  t_554
 * graph export `file'-TOther.png, replace 

//	#9
//	Direct effects
xtreg `dv' `iv' `iv2' `cont', fe cluster(CID) 
predict yhat_dir, xb 
margins, at(`iv'=(2(2)8))
/*
matrix b=r(b)
matrix at=r(at)

* Display the original matrix b
matrix list b
* Get dimensions of matrix b
local rowdim = 1  
local coldim = 24 
* Create a new matrix to store the transposed matrix
matrix b_transposed = J(`coldim', `rowdim', .)
* Loop through each element of the original matrix and fill the transposed matrix
forval i = 1 / `rowdim' {
    forval j = 1 / `coldim' {
        matrix b_transposed[`j', `i'] = b[`i', `j']
    }
}


matrix list at
matrix list b_transposed
*/

xtreg `dv' `iv' `iv2' `cont' if Lfraser==1, fe cluster(CID) 
predict Lyhat, xb 
margins, at(`iv'=(2(2)8))

/*
matrix b=r(b)
matrix at=r(at)

* Display the original matrix b
matrix list b
* Get dimensions of matrix b
local rowdim = 1  
local coldim = 24 
* Create a new matrix to store the transposed matrix
matrix b_transposed = J(`coldim', `rowdim', .)
* Loop through each element of the original matrix and fill the transposed matrix
forval i = 1 / `rowdim' {
    forval j = 1 / `coldim' {
        matrix b_transposed[`j', `i'] = b[`i', `j']
    }
}


matrix list at
matrix list b_transposed
*/
xtreg `dv' `iv' `iv2' `cont' if Lfraser==0, fe cluster(CID) 
predict Hyhat, xb 
margins, at(`iv'=(2(2)8))

* graph twoway (lfit yhat_dir fraser) (lfit Lyhat fraser) (lfit Hyhat fraser), ///
	xtitle("Fraser") ytitle("Predicted Ln Homicide Rates per 100k pop") ///
	note("Best fit line | `note'") legend(label(1 "Fraser") label(2 "Low Fraser") label(3 "High Fraser"))
* graph export `file'-FraserDirect.png, replace 

//	#10
//	Moderating effects Set up for * graphing 
xtreg `dv' c.`iv'##c.`iv2' c.`iv'##c.lgdp `cont', fe cluster(CID) 
margins, at(`iv2'=(1(10)51) `iv'=(2(2)8))
/*
matrix b=r(b)
matrix at=r(at)

* Display the original matrix b
matrix list b
* Get dimensions of matrix b
local rowdim = 1  
local coldim = 24 
* Create a new matrix to store the transposed matrix
matrix b_transposed = J(`coldim', `rowdim', .)
* Loop through each element of the original matrix and fill the transposed matrix
forval i = 1 / `rowdim' {
    forval j = 1 / `coldim' {
        matrix b_transposed[`j', `i'] = b[`i', `j']
    }
}


matrix list at
matrix list b_transposed
*/

margins, at(lgdp=(6(2)12) `iv'=(2(2)8))
/*
matrix b=r(b)
matrix at=r(at)

* Display the original matrix b
matrix list b
* Get dimensions of matrix b
local rowdim = 1  
local coldim = 16 
* Create a new matrix to store the transposed matrix
matrix b_transposed = J(`coldim', `rowdim', .)
* Loop through each element of the original matrix and fill the transposed matrix
forval i = 1 / `rowdim' {
    forval j = 1 / `coldim' {
        matrix b_transposed[`j', `i'] = b[`i', `j']
    }
}


matrix list at
matrix list b_transposed
*/


xtreg `dv'  c.`iv'##c.lgdp `iv2' `cont', fe cluster(CID) 
margins, at(lgdp=(6(2)12) `iv'=(2(2)8))
/*
matrix b=r(b)
matrix at=r(at)

* Display the original matrix b
matrix list b
* Get dimensions of matrix b
local rowdim = 1  
local coldim = 16 
* Create a new matrix to store the transposed matrix
matrix b_transposed = J(`coldim', `rowdim', .)
* Loop through each element of the original matrix and fill the transposed matrix
forval i = 1 / `rowdim' {
    forval j = 1 / `coldim' {
        matrix b_transposed[`j', `i'] = b[`i', `j']
    }
}


matrix list at
matrix list b_transposed
*/

//	#10
//	Data for moderating effects (obtained from above) 
preserve 
insheet using 00-Market-InteractionData.csv, comma clear n
* graph twoway (connected yhat_infantxfraser infantmort if fraser==2, msymbol(i) lpatter(solid) lcolor(black)) ///
	(connected yhat_infantxfraser infantmort if fraser==8, msymbol(i)  lpatter(dash_dot) lcolor(black)), ///
	ytitle("Predicted Ln Homicide Rates per 100k pop") xtitle("Infant Mortality Rates") ///
	title("Fraser X Infant Mortality") name(FXI, replace) ///
	legend(label(1 "Low Fraser") label(2 "High Fraser"))
	
	
* graph twoway (connected yhat_gdpfraser lgdp if fraser==2, msymbol(i) lpatter(solid) lcolor(black)) ///
	(connected yhat_gdpfraser lgdp if fraser==8, msymbol(i)  lpatter(dash_dot) lcolor(black)), ///
	ytitle("Predicted Ln Homicide Rates per 100k pop") xtitle("Ln GDP") ///
	title("Ln GDP X Fraser") name(FXG, replace) ///
	legend(label(1 "Low Fraser") label(2 "High Fraser"))

* graph combine FXI FXG, altshrink  ycommon
* graph export `file'-interaction.png, replace 

* graph twoway connected  yhat_fraserdir yhat_lowfraser yhat_highfraser fraser, ///
	ytitle("Predicted Ln Homicide Rates per 100k pop") xtitle("Fraser") ///
	legend(label (1 "Fraser") label(2 "Low Fraser") label(3 "High Fraser")) ///
	note("Predicted Prob ||`note'") 
* graph export `file'-direct2.png, replace 
restore 

//	#11
//	save 
save `file'.dta, replace 

//	#12
//	Cleaning up for R 
keep  CID nation year regioncode regionname countrycode rhom `dv' `iv' `iv2' `cont' lgdp gdp KOFGI heritage rol LAC Lfraser trust notrust yhat_dir Lyhat Hyhat
saveold `file'-old.dta, v(11) replace 
collapse (mean) rhom `dv' `iv' `iv2' `cont' Lfraser lgdp gdp KOFGI heritage rol trust notrust, by(year) 
saveold `file'-avg.dta, v(11) replace
log close 
//


